import { BaseLayer } from "../baseLayer";

export class BaseTileLayer extends BaseLayer {
  constructor(options = {}) {
    super(options)
  }

  /**
   * 图层顺序，数字大的在上面。（当hasZIndex为true时）
   * @type {Number}
   */
  get zIndex() {
    return this.options.zIndex
  }
  set zIndex(zIndex) {
    this.options.zIndex = zIndex

    if (this._imageryLayer == null || zIndex == null) {
      return
    }

    //先移动到最顶层
    this._map.imageryLayers.raiseToTop(this._imageryLayer)

    let layers = this._map.imageryLayers._layers
    for (let i = layers.length - 1; i >= 0; i--) {
      let imageLayer = layers[i]
      if (imageLayer == this._imageryLayer) {
        continue
      }
      if (imageLayer.zIndex) {
        if (zIndex < imageLayer.zIndex) {
          this._map.imageryLayers.lower(this._imageryLayer) //下移一个位置
        }
      }
    }
  }

  _createImageryProvider (options) {
    return new Cesium.UrlTemplateImageryProvider(options)
  }

  // 将对象添加到地图上的钩子函数
  _addedHook () {
    let imageryProvider = this._createImageryProvider({
      ...this.options,
    })
    if (!Cesium.defined(imageryProvider)) {
      return
    }

    let options = this.options

    let layerOpts = {
      ...options,
    }
    this._imageryLayer = new Cesium.ImageryLayer(imageryProvider, layerOpts)

    this._map.imageryLayers.add(this._imageryLayer)
  }
  /**
 * 对象从地图上移除的创建钩子方法，
 * 每次remove时都会调用
 * @return {void}  无
 */
  _removedHook () {
    this._map.imageryLayers.remove(this._imageryLayer, true)
    this._imageryLayer = null
  }
}